/**
 * 用户相关接口
 * (未测试)
 * @authors 杨兴洲（of2502）
 * @date    2017/3/13 10:27
 * @version 1.0
 */

import * as rp from "request-promise-native";
const HOST = 'https://api.weixin.qq.com/cgi-bin';

export default class WeixinUserService {

    /**
     * 获取公众号已创建的标签
     * @returns {Promise<any>}
     */
    static getTags(access_token): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/get`,
            qs: {access_token},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 新增标签
     */
    static createTag({access_token, tagName}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/create`,
            qs: {access_token},
            body: {
                tag: {name: tagName}
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 编辑标签
     */
    static updateTag({access_token, tagName, tagID}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/update`,
            qs: {access_token},
            body: {
                tag: {
                    id: tagID,
                    name: tagName
                }
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 删除标签
     */
    static deleteTag({access_token, tagID}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/delete`,
            qs: {access_token},
            body: {
                tag: {id: +tagID}
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 获取某标签下的用户列表
     * @param access_token
     * @param tagID
     * @param next_openid
     * @returns {Promise<any>}
     */
    static getListByTag({access_token, tagID, next_openid}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/user/tag/get`,
            qs: {access_token},
            body: {
                tagid: tagID,
                next_openid
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 获取粉丝列表
     * @param access_token
     * @param next_openid
     * @returns {Promise<any>}
     */
    static getList({access_token, next_openid}: { access_token: string, next_openid?: string }): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/user/get`,
            qs: {access_token, next_openid},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 获取用户身上的标签列表
     * @param access_token
     * @param openid
     * @returns {Promise<any>}
     */
    static getUserTags({access_token, openid}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/getidlist`,
            qs: {access_token},
            body: {openid},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 批量为用户打标签
     * @param access_token
     * @param tagID
     * @param openid_list
     * @returns {Promise<any>}
     */
    static batchTagging({access_token, tagID, openid_list}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/members/batchtagging`,
            qs: {access_token},
            body: {openid_list, tagid: tagID},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     *  批量为用户取消标签
     * @param access_token
     * @param tagID
     * @param openid_list
     * @returns {Promise<any>}
     */
    static batchUntagging({access_token, tagID, openid_list}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/members/batchuntagging`,
            qs: {access_token},
            body: {openid_list, tagid: tagID},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 设置用户备注名
     * /user/info/updateremark
     */
    static updateRemark({access_token, openid, remark}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/user/info/updateremark`,
            qs: {access_token},
            body: {openid, remark},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 黑名单列表
     * @param access_token
     * @param begin_openid
     * @returns {Promise<any>}
     */
    static getBlacklist({access_token, begin_openid}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/members/getblacklist`,
            qs: {access_token},
            body: {begin_openid},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 批量拉黑/Black
     * @param access_token {String}
     * @param openid_list {Array}
     * @returns {Promise<any>}
     * @constructor
     */
    static batchBlacklist({access_token, openid_list}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/members/batchblacklist`,
            qs: {access_token},
            body: {openid_list},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 批量取消拉黑/Unblack
     * @param access_token
     * @param openid_list
     * @returns {Promise<any>}
     */
    static batchUnblacklist({access_token, openid_list}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/tags/members/batchunblacklist`,
            qs: {access_token},
            body: {openid_list},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 获取粉丝详情
     * @param access_token
     * @param openid
     * @returns {Promise<any>}
     */
    static getUserInfo({access_token, openid}): Promise<any> {
        return rp({
            method: 'GET',
            uri: `${HOST}/user/info`,
            qs: {access_token, openid},
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }

    /**
     * 批量获取粉丝详情
     * @param access_token
     * @param openid_list
     * @param lang
     * @returns {Promise<any>}
     */
    static batchGetUserInfo({access_token, openid_list, lang = "zh_CN"}): Promise<any> {
        return rp({
            method: 'POST',
            uri: `${HOST}/user/info/batchget`,
            qs: {access_token},
            body: {
                user_list: openid_list.map(openid => ({openid, lang}))
            },
            json: true
        })
            .then((data: any) => data.errcode ? Promise.reject(data) : data);
    }
}
